*--------------------------------------------------
* Define programs
*--------------------------------------------------
clear programs

program define event_study95
	cap drop _b_
	cap drop _min_
	cap drop _max_
	cap drop _year_
	gen _b_=.
	gen _min_=.
	gen _max_=.

	sum fyear if e(sample)
	loc tmin=r(min)
	loc tmax=r(max)


	local tmin1 = `tmin' + 1
	loc t_nyears = `tmax' - `tmin1' + 1
	loc t_nyears = `tmax' - `tmin' + 1
	gen _year_ = _n + `tmin' - 1 in 1/`t_nyears'
	loc i=0
	forval y=`tmin'/`tmax'{
	  loc ++i
	  *if `y'==2010{
	  if `y'==2010{
		   replace _b_= 0 in `i'
		   replace _min_ = . in `i'
		   replace _max_ = . in `i'
		   continue
		}
	  replace _b_ = _b[`0'_X`y']           in `i'
	  replace _min_=_b_-1.96*_se[`0'_X`y'] in `i'
	  replace _max_=_b_+1.96*_se[`0'_X`y'] in `i'
	}

	tw ( rcap _min_ _max_ _year_ ) ( scatter _b_ _year_, m(o) c(l) lp(line) ) ///
		if inrange(_year_,`tmin',`tmax'),  ///
		legend(off) xtitle("Year", size(large)) ytitle("") ///
		xlabel(`tmin'(1)`tmax',angle(60) labsize(large)) ///
		ylabel(,labsize(large) angle(horizontal)) ///
		yline(0, lcolor(black) lpattern(dash)) ///
		xline(2010, lcolor(black) lpattern(dash)) graphregion(color(white))
end

program define iv_event_study95
	cap drop _b_
	cap drop _min_
	cap drop _max_
	cap drop _year_
	gen _b_=.
	gen _min_=.
	gen _max_=.

	sum fyear if e(sample)
	loc tmin=r(min)
	loc tmax=r(max)


	local tmin1 = `tmin' + 1
	loc t_nyears = `tmax' - `tmin1' + 1
	loc t_nyears = `tmax' - `tmin' + 1
	gen _year_ = _n + `tmin' - 1 in 1/`t_nyears'
	loc i=0
	forval y=`tmin'/`tmax'{
	  loc ++i
	  *if `y'==2010{
	  if `y'==2010{
		   replace _b_= 0 in `i'
		   replace _min_ = . in `i'
		   replace _max_ = . in `i'
		   continue
		}
	  replace _b_ = _b[`0'_X`y']           in `i'
	  replace _min_=_b_-1.96*_se[`0'_X`y'] in `i'
	  replace _max_=_b_+1.96*_se[`0'_X`y'] in `i'
	}

	tw ( rcap _min_ _max_ _year_ ) ( scatter _b_ _year_, m(o) c(l) lp(line) ) ///
		if inrange(_year_,`tmin',`tmax'),  ///
		legend(off) xtitle("Year", size(large)) ytitle("") ///
		xlabel(`tmin'(1)`tmax',angle(60) labsize(large)) ///
		ylabel(,labsize(large)) ///
		yline(0, lcolor(black) lpattern(dash)) ///
		xline(2010, lcolor(black) lpattern(dash)) graphregion(color(white))
end


program define eventstudy_hetero
	local auditvar   `1'
	local jkauditvar `2'
	local heterovar  `3'
	local hospvar    `4'

	di "now making event study for `heterovar'..."
	cap drop t_*
	elabel list (`heterovar')

	local values = r(values)
	local labels = r(labels)

	loc j = 1
	foreach label of local labels{
		local label`j' "`label'"
		loc ++j
	}
	di "creating yearly vars for regression..."
	forval y = 2007/2015{
		
		if `y' != 2010{
			local i = 1
			foreach value of local values{
				if `i' != 1{
					gen t_audit_hetero_`i'_X`y' = `auditvar'_X`y' * (`heterovar' == `value')
					gen t_jk_audit_hetero_`i'_X`y' = `jkauditvar'_X`y' * (`heterovar' == `value')
				} //i
				local i = `i'+1
				
			} // value
		} // not 2010
	} //years

	ivreghdfe  `hospvar' (`auditvar'_X2007 `auditvar'_X2008 `auditvar'_X2009 `auditvar'_X2011 `auditvar'_X2012 `auditvar'_X2013 `auditvar'_X2014 `auditvar'_X2015 t_audit_hetero* ///
	 			= `jkauditvar'_X2007 `jkauditvar'_X2008 `jkauditvar'_X2009 `jkauditvar'_X2011 `jkauditvar'_X2012 `jkauditvar'_X2013 `jkauditvar'_X2014 `jkauditvar'_X2015 t_jk_audit_hetero*) ///
				,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)

	cap drop _b*
	cap drop _min*
	cap drop _max*
	cap drop _year*

	sum fyear if e(sample)
	loc tmin=r(min)
	loc tmax=r(max)

	elabel list (`heterovar')
	local values = r(values)

	local tmin1 = `tmin' + 1
	loc t_nyears = `tmax' - `tmin1' + 1
	loc t_nyears = `tmax' - `tmin' + 1
	gen _year1_ = _n + `tmin' - 1 in 1/`t_nyears'

	loc i = 0
	local j = 1
	foreach value of local values{
		di "now doing value: `value'"
		if `j' == 1{
			gen _b1_ = .
			gen _min1_ = .
			gen _max1_ = .
		}
		if `j'!= 1{
			gen _b`j'_  = .
			gen _min`j'_ = .
			gen _max`j'_ = .
			local jmin1 = `j'-1
			gen _year`j'_ = _year1_ + 0.1 * `jmin1'
			replace _year`j'_ = 2010 if _year1_ == 2010
		}
		local ++j
	}



	forval y = `tmin'/`tmax'{
		loc ++i
		local j = 1
		
		foreach value of local values{
			if `j' == 1{
				if `y' == 2010{
				replace _b1_ = 0 in `i'
				replace _min1_ = . in `i'
				replace _max1_ = . in `i'
				}
				if `y' != 2010{
					replace _b1_ = _b[audit_in2011_X`y'] in `i'
					replace _min1_ = _b1_ - 1.96*_se[audit_in2011_X`y'] in `i'
					replace _max1_ = _b1_ + 1.96*_se[audit_in2011_X`y'] in `i'
				}
			}
			if `j'!= 1{
				if `y' == 2010{
					replace _b`j'_ = 0 in `i'
					replace _min`j'_ = . in `i'
					replace _max`j'_ = . in `i'
					*replace _year`j'_ = 2010 in `i'
					*continue
				}
				if `y'!= 2010{
					replace _b`j'_ = _b[t_audit_hetero_`j'_X`y'] in `i'
					replace _min`j'_ = _b`j'_ - 1.96*_se[t_audit_hetero_`j'_X`y'] in `i'
					replace _max`j'_ = _b`j'_ + 1.96*_se[t_audit_hetero_`j'_X`y'] in `i'
				}
			}
			local ++j
		}
	}
	local color1 = "dknavy"
	local color2 = "maroon"
	local color3 = "dkgreen"

	local twoway_command ""
	local legend_command "legend(order("
	local j = 1
	foreach value of local values{
		di "`color`j''"
		local twoway_command "`twoway_command' ( rcap _min`j'_ _max`j'_ _year`j'_ , lcolor(`color`j'')) ( scatter _b`j'_ _year`j'_, m(o) c(l) lp(line) color(`color`j''))"
		local twoj = 2*`j'
		local legend_command "`legend_command' `twoj' `"`label`j''"'"
		local ++j
	}
	local legend_command "`legend_command'))"


	tw `twoway_command' ///
		if inrange(_year1_,`tmin',`tmax') ///
		, `legend_command' xtitle("Year", size(large)) ytitle("") ///
		xlabel(`tmin'(1)`tmax',angle(60) labsize(large)) ///
		ylabel(,labsize(large) angle(horizontal)) ///
		yline(0, lcolor(black) lpattern(dash)) ///
		xline(2010, lcolor(black) lpattern(dash)) graphregion(color(white))
end

cap program drop table_hetero
program define table_hetero
	local auditvar   `1'
	local jkauditvar `2'
	local heterovar  `3'
	local hospvar    `4'

	di "now making event study for `heterovar'..."
	cap drop t_*
	elabel list (`heterovar')

	local values = r(values)
	local labels = r(labels)

	loc j = 1
	foreach label of local labels{
		local label`j' "`label'"
		di "`label`j''"
		loc ++j
	}
	label var audit_in2011_Xpost "2011 audit rate $\times$ post $\times$ `label1'"
	di "creating hetero post var for regression..."
		local i = 1
		foreach value of local values{
			if `i' != 1{
				gen t_audit_hetero_`i'_Xpost = `auditvar'_Xpost * (`heterovar' == `value')
				label var t_audit_hetero_`i'_Xpost "2011 audit rate $\times$ post $\times$ `label`i''"
				gen t_jk_audit_hetero_`i'_Xpost = `jkauditvar'_Xpost * (`heterovar' == `value')
			} //i
			local i = `i'+1
			
		} // value

	ivreghdfe  `hospvar' (`auditvar'_Xpost t_audit_hetero* ///
	 			= `jkauditvar'_Xpost t_jk_audit_hetero*) ///
				,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
end

cap program drop table_hetero_reduced
program define table_hetero_reduced
	local auditvar   `1'
	local jkauditvar `2'
	local heterovar  `3'
	local hospvar    `4'

	di "now making event study for `heterovar'..."
	cap drop t_*
	elabel list (`heterovar')

	local values = r(values)
	local labels = r(labels)

	loc j = 1
	foreach label of local labels{
		local label`j' "`label'"
		di "`label`j''"
		loc ++j
	}
	label var audit_in2011_Xpost "2011 audit rate $\times$ post $\times$ `label1'"
	label var `jkauditvar'_Xpost "2011 jk audit rate $\times$ post $\times$ `label1'"
	di "creating hetero post var for regression..."
		local i = 1
		foreach value of local values{
			if `i' != 1{
				gen t_audit_hetero_`i'_Xpost = `auditvar'_Xpost * (`heterovar' == `value')
				label var t_audit_hetero_`i'_Xpost "2011 audit rate $\times$ post $\times$ `label`i''"
				gen t_jk_audit_hetero_`i'_Xpost = `jkauditvar'_Xpost * (`heterovar' == `value')
				label var t_jk_audit_hetero_`i'_Xpost "2011 jk audit rate $\times$ post $\times$ `label`i''"
			} //i
			local i = `i'+1
			
		} // value

	reghdfe  `hospvar' `jkauditvar'_Xpost t_jk_audit_hetero* ///
				,absorb(hospcomp_group_fyear pn) cluster(state nearseg100_state_2)
end



// Standardized scale for log_n_claims
cap program drop iv_event_study95_yscale
program define iv_event_study95_yscale
	* `1' = variable
	* `2' = min of y
	* `3' = max of y
	* `4' = unit

	local min = `2'
	local max = `3'
	local unit = `4'
	cap drop _b_
	cap drop _min_
	cap drop _max_
	cap drop _year_
	gen _b_=.
	gen _min_=.
	gen _max_=.

	sum fyear if e(sample)
	loc tmin=r(min)
	loc tmax=r(max)


	local tmin1 = `tmin' + 1
	loc t_nyears = `tmax' - `tmin1' + 1
	loc t_nyears = `tmax' - `tmin' + 1
	gen _year_ = _n + `tmin' - 1 in 1/`t_nyears'
	loc i=0
	forval y=`tmin'/`tmax'{
	  loc ++i
	  *if `y'==2010{
	  if `y'==2010{
		   replace _b_= 0 in `i'
		   replace _min_ = . in `i'
		   replace _max_ = . in `i'
		   continue
		}
	  replace _b_ = _b[`1'_X`y']           in `i'
	  replace _min_=_b_-1.96*_se[`1'_X`y'] in `i'
	  replace _max_=_b_+1.96*_se[`1'_X`y'] in `i'
	}

	tw ( rcap _min_ _max_ _year_ ) ( scatter _b_ _year_, m(o) c(l) lp(line) ) ///
		if inrange(_year_,`tmin',`tmax'),  ///
		legend(off) xtitle("Year", size(large)) ytitle("") ///
		xlabel(`tmin'(1)`tmax',angle(60) labsize(large)) ///
		ylabel(`min'(`unit')`max',labsize(large)) ///
		yline(0, lcolor(black) lpattern(dash)) ///
		yscale(range(`min' `max')) ///
		xline(2010, lcolor(black) lpattern(dash)) graphregion(color(white))
end

// all for custom yscale
cap program drop iv_event_study95_yscale_title
program define iv_event_study95_yscale_title
	* `1' = variable
	* `2' = min of y
	* `3' = max of y
	* `4' = unit
	* `5' = title

	local min = `2'
	local max = `3'
	local unit = `4'
	cap drop _b_
	cap drop _min_
	cap drop _max_
	cap drop _year_
	gen _b_=.
	gen _min_=.
	gen _max_=.

	sum fyear if e(sample)
	loc tmin=r(min)
	loc tmax=r(max)


	local tmin1 = `tmin' + 1
	loc t_nyears = `tmax' - `tmin1' + 1
	loc t_nyears = `tmax' - `tmin' + 1
	gen _year_ = _n + `tmin' - 1 in 1/`t_nyears'
	loc i=0
	forval y=`tmin'/`tmax'{
	  loc ++i
	  *if `y'==2010{
	  if `y'==2010{
		   replace _b_= 0 in `i'
		   replace _min_ = . in `i'
		   replace _max_ = . in `i'
		   continue
		}
	  replace _b_ = _b[`1'_X`y']           in `i'
	  replace _min_=_b_-1.96*_se[`1'_X`y'] in `i'
	  replace _max_=_b_+1.96*_se[`1'_X`y'] in `i'
	}

	tw ( rcap _min_ _max_ _year_ ) ( scatter _b_ _year_, m(o) c(l) lp(line) ) ///
		if inrange(_year_,`tmin',`tmax'),  ///
		legend(off) xtitle("Year", size(large)) ytitle("") ///
		xlabel(`tmin'(1)`tmax',angle(60) labsize(large)) ///
		ylabel(`min'(`unit')`max',labsize(large)) ///
		yline(0, lcolor(black) lpattern(dash)) ///
		yscale(range(`min' `max')) ///
		xline(2010, lcolor(black) lpattern(dash)) graphregion(color(white)) ///
		title("`5'")
end
